/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.shared.model;

import org.jb.sharedbills.shared.model.notification.AbstractNotifier;
import org.jb.sharedbills.shared.model.notification.Notification;
import org.jb.sharedbills.shared.model.notification.NotificationListener;

/**
 * 
 * @author jeremie
 *
 */
//@Embeddable
public class Bill extends AbstractNotifier {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2509590998773712564L;
	private String name;
	private Member from;
	private Group to;
	private double amount;
	private transient boolean registered = false;
	
	/**
	 * 
	 */
	public Bill() {
		
	}
	
	/**
	 * 
	 * @param from
	 * @param to
	 * @param amount
	 * @param name
	 */
	public Bill( Member from, Group to, double amount, String name ) {
		setFrom( from );
		setTo( to );
		setAmount( amount );
		setName( name );
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setName(String name) {
		if (this.name == null || !this.name.equals(name)) {
			String oldName = this.name;
			this.name = name;
			doNotify( "name", oldName, this.name );
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * 
	 * @param from
	 */
	public void setFrom(Member from) {
		if (this.from != from) {
			Member oldfrom = this.from;
			if (oldfrom != null) {
				oldfrom.removeListener(this);
			}
			this.from = from;
			if (from != null && hasListeners()) {
				from.addListener(this);
			}
			doNotify( "from", oldfrom, this.from );
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public Member getFrom() {
		return from;
	}
	
	/**
	 * 
	 * @param to
	 */
	public void setTo(Group to) {
		if (this.to != to) {
			Group oldto = this.to;
			if (oldto != null) {
				oldto.removeListener(this);
			}
			this.to = to;
			if (to != null && hasListeners()) {
				to.addListener(this);
			}
			doNotify( "to", oldto, this.to );
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public Group getTo() {
		return to;
	}
	
	/**
	 * 
	 * @param amount
	 */
	public void setAmount(double amount) {
		if (amount != this.amount) {
			
			double oldamount = this.amount;
			this.amount = amount;
			doNotify( "amount", oldamount, amount );

		}
	}
	
	/**
	 * 
	 * @return
	 */
	public double getAmount() {
		return amount;
	}

	@Override
	public void handle(Notification notification) {
		if (notification.getSender() == from && notification.getProperty().equals("name")) {
			doNotify( "from_name", null, null );
		}
		if (notification.getSender() == to && notification.getProperty().equals("name")) {
			doNotify( "to_name", null, null );
		}
		super.handle(notification);
	}

	@Override
	public void addListener(NotificationListener listener) {
		if (!registered) {
			registered = true;
			
			if( from != null)
				from.addListener( this );
			
			if (to != null)
				to.addListener( this );
		}
		super.addListener(listener);
	}
	
	
}
